﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// **********************************************************************
// Copyright (C) XM
// Author: 赵思佳
// Date: 2019-01-17
// Desc: 代码逻辑文件
// **********************************************************************
namespace SpaceDFramework
{
    public class MsgDispacther<T>:MonoBehaviour
    {
        static Dictionary<string, Action<T>> mRegisterMsgs = new Dictionary<string, Action<T>>();
        public static void Register(string msgName, Action<T> onMsgReceived) {
            if (!mRegisterMsgs.ContainsKey(msgName))
                mRegisterMsgs.Add(msgName, _=> { });
            mRegisterMsgs[msgName] += onMsgReceived;
        }
        public static void UnRegister(string msgName,Action<T> onMsgReceived)
        {
            if (mRegisterMsgs.ContainsKey(msgName))
                mRegisterMsgs[msgName] -= onMsgReceived;
        }
        public static void UnRegister(string msgName)
        {
            if (mRegisterMsgs.ContainsKey(msgName))
                mRegisterMsgs.Remove(msgName);
        }
        public static void Send(string msgName, T data) {
            if(mRegisterMsgs.ContainsKey(msgName))
                mRegisterMsgs[msgName](data);
        }
    }
}
